﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WinformMapXpSupportDemo.Forms;
using Chromium.Remote;
using WinformMapXpSupportDemo.Model;

namespace WinformMapXpSupportDemo
{
    public partial class MainForm : Form
    {

        public MapForm GaoDeMapForm { get; set; }

        public readonly AMapJsObject _AmapJsObject;

        private  AddDeviceForm _AddDeviceForm;
        
        public MainForm()
        {
            InitializeComponent();
            //添加地图组建
            _AmapJsObject = new AMapJsObject(this);
            GaoDeMapForm = new MapForm();
            GaoDeMapForm.TopLevel = false;
            GaoDeMapForm.Dock = DockStyle.Fill;
            this.panelMap.Controls.Add(GaoDeMapForm);
            GaoDeMapForm.Show();
            //初始化地图JS对象
            InitMapJsObject();
            //绑定JS对象事件
            BindMapJsObjectEvent();

        }
        /// <summary>
        /// 绑定地图Js对象和winfrom的交互事件
        /// </summary>
        private void BindMapJsObjectEvent()
        {
            //地理地址改变事件
            _AmapJsObject.OnAddressChanged += (sender, e) =>
            {
                this.txtAddress.Text = (string)e.Tag;
                _AddDeviceForm?.UpdateAddress(this.txtAddress.Text);
            };
            //经纬度坐标改变事件
            _AmapJsObject.OnMapPositionChanged += (sender, e) =>
            {
                MapPosition position = e.Tag as MapPosition;
                if (position == null)
                {
                    return;
                }
                this.txtLng.Text = position.Longitude.ToString();
                this.txtLat.Text = position.Latitude.ToString();
            };
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            //地图组件事件绑定，
            GaoDeMapForm.BrowserCore.LoadHandler.OnLoadStart += LoadHandler_OnLoadStart;
            GaoDeMapForm.BrowserCore.LoadHandler.OnLoadEnd += LoadHandler_OnLoadEnd;
            GaoDeMapForm.BrowserCore.LoadHandler.OnLoadError += LoadHandler_OnLoadError;
        }

        private void LoadHandler_OnLoadEnd(object sender, Chromium.Event.CfxOnLoadEndEventArgs e)
        {
            this.toolStripStatusLabel1.Text = "地图加载完毕";
            //GaoDeMapForm.BrowserCore.ShowDevTools();
        }

        private void LoadHandler_OnLoadStart(object sender, Chromium.Event.CfxOnLoadStartEventArgs e)
        {
            this.toolStripStatusLabel1.Text = "正在加载地图";
        }

        private void LoadHandler_OnLoadError(object sender, Chromium.Event.CfxOnLoadErrorEventArgs e)
        {
            MessageBox.Show("地图加载失败","系统提示",MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if(this.listView1.SelectedItems.Count <=0 || listView1.SelectedItems[0] == null)
            {
                return;
            }
            DeviceInfo selectedDeviceInfo = listView1.SelectedItems[0].Tag as DeviceInfo;
            if(selectedDeviceInfo == null)
            {
                return;
            }
            GaoDeMapForm.BrowserCore.ExecuteJavascript($"showDeviceDetails('{selectedDeviceInfo.DeviceCode}')");
        }
    }
}
